package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Warnnings;
import ex.com.jm.food.service.AdminWarnService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class AdminWarnAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private AdminWarnService adminWarnService;
	private Warnnings warnnings;
	private String comName;
	private String s_date;
	private String e_date;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private Map<String, String> dateMap;
	public AdminWarnService getAdminWarnService() {
		return adminWarnService;
	}
	public void setAdminWarnService(AdminWarnService adminWarnService) {
		this.adminWarnService = adminWarnService;
	}
	public Warnnings getWarnnings() {
		return warnnings;
	}
	public void setWarnnings(Warnnings warnnings) {
		this.warnnings = warnnings;
	}
	public String getComName() {
		return comName;
	}
	public void setComName(String comName) {
		this.comName = comName;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 到达警告库管理
	 * @return
	 */
	public String warnList(){
		dateMap=DateUtils.getDate();
		return "warnList";
	}
	/**
	 * 获取数据
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getWarns(){
		int offset=(initPage-1)*pageRow;
		List<Warnnings> warnnings=this.getAdminWarnService().getWarnnings(offset, pageRow, comName, s_date, e_date);
		//JSONArray jsonwarn=JSONArray.fromObject(warnnings);
		List<Map<String, Object>> listMaps=new ArrayList<Map<String,Object>>();
		for (int i = 0; i < warnnings.size(); i++) {
			Map map=(Map) warnnings.get(i);
			/*System.out.println(map.size());
			System.err.println(map.get("com_name"));
			System.err.println(map.get("warn_why"));*/
			listMaps.add(map);
		}
		JSONArray json=JSONArray.fromObject(listMaps);
		totalCount=this.getAdminWarnService().warnCount(comName, s_date, e_date);
		JSONObject jo=JsonUtils.buildJson(json, initPage, pageRow, totalCount);
		//System.out.println(jo);
		System.out.println("执行");
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
}
